package com.stylefeng.guns.modular.commodity.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.commodity.model.Commodity;
import com.stylefeng.guns.modular.commodity.service.ICommodityService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-12 17:02:08
 */
@Controller
@RequestMapping("/commodity")
public class CommodityController extends BaseController {

    private String PREFIX = "/commodity/commodity/";

    @Autowired
    private ICommodityService commodityService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "commodity.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/commodity_add")
    public String commodityAdd() {
        return PREFIX + "commodity_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/commodity_update/{commodityId}")
    public String commodityUpdate(@PathVariable Integer commodityId, Model model) {
        Commodity commodity = commodityService.selectById(commodityId);
        model.addAttribute("item",commodity);
        LogObjectHolder.me().set(commodity);
        return PREFIX + "commodity_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return commodityService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Commodity commodity) {
        commodityService.insert(commodity);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer commodityId) {
        commodityService.deleteById(commodityId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Commodity commodity) {
        commodityService.updateById(commodity);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{commodityId}")
    @ResponseBody
    public Object detail(@PathVariable("commodityId") Integer commodityId) {
        return commodityService.selectById(commodityId);
    }

    @RequestMapping(value = "/list/{sortName}")
    public Object listByCommodityName(@PathVariable("sortName") String sortName){
        EntityWrapper<Commodity> commodityWrapper = new EntityWrapper<>();
        commodityWrapper.where("name = {0}",sortName);
        return commodityService.selectList(commodityWrapper);
    }


}
